using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoUpdate.Models;
using System.Data.SqlClient;
using System.Data;

namespace AutoUpdate.DAL
{
    [Serializable]
    public static class T_SysUpdateService
    {
        /// <summary>
        /// 根据主键返回对象
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>对象</returns>
        /// <exception cref="有异常抛出">有异常抛出</exception>
        public static AutoUpdate.Models.T_SysUpdate GetObjectById(string id)
        {
            string sql = "select * from T_SysUpdate where ID = @id";
            SqlParameter[] paras =
            {
                new SqlParameter("@id",id)
                };
            try
            {
                SqlDataReader reader = SQLHelper.ExecuteReader(sql, paras);
                if (reader.Read())
                {
                    AutoUpdate.Models.T_SysUpdate g = new AutoUpdate.Models.T_SysUpdate();
                    g.ID = reader["ID"] == DBNull.Value ? 0 : Convert.ToInt32(reader["ID"]);
                    g.FID = reader["FID"] == DBNull.Value ? "" : Convert.ToString(reader["FID"]);
                    g.FOrder = reader["FOrder"] == DBNull.Value ? 0 : Convert.ToInt32(reader["FOrder"]);
                    g.FProductID = reader["FProductID"] == DBNull.Value ? "" : Convert.ToString(reader["FProductID"]);
                    g.FFieldName = reader["FFieldName"] == DBNull.Value ? "" : Convert.ToString(reader["FFieldName"]);
                    g.FModifyTime = reader["FModifyTime"] == DBNull.Value ? "" : Convert.ToString(reader["FModifyTime"]);
                    g.FFileType = reader["FFileType"] == DBNull.Value ? null : (byte[])(reader["FFileType"]);
                    g.FFileSize = reader["FFileSize"] == DBNull.Value ? "" : Convert.ToString(reader["FFileSize"]);
                    g.FInstallPath = reader["FInstallPath"] == DBNull.Value ? "" : Convert.ToString(reader["FInstallPath"]);
                    g.FPublishTime = reader["FPublishTime"] == DBNull.Value ? new Nullable<DateTime>() : Convert.ToDateTime(reader["FPublishTime"]);
                    g.FLastClientIP = reader["FLastClientIP"] == DBNull.Value ? "" : Convert.ToString(reader["FLastClientIP"]);
                    g.FLastTime = reader["FLastTime"] == DBNull.Value ? new Nullable<DateTime>() : Convert.ToDateTime(reader["FLastTime"]);
                    g.FDownNum = reader["FDownNum"] == DBNull.Value ? 0 : Convert.ToInt32(reader["FDownNum"]);
                    g.FMemo = reader["FMemo"] == DBNull.Value ? "" : Convert.ToString(reader["FMemo"]);
                    g.FFlag = reader["FFlag"] == DBNull.Value ? null : (byte[])(reader["FFlag"]);
                    g.exefile = reader["exefile"] == DBNull.Value ? null : (byte[])(reader["exefile"]);
                    g.FMustUpdate = reader["FMustUpdate"] == DBNull.Value ? 0 : Convert.ToInt32(reader["FMustUpdate"]);
                    g.FNowUpdate = reader["FNowUpdate"] == DBNull.Value ? "" : Convert.ToString(reader["FNowUpdate"]);

                    return g;
                }

            }
            catch
            {

                throw;
            }
            return null;
        }
        /// <summary>
        /// 根据条件查询多个，返回List集合
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="paras">参数</param>
        /// <returns>List集合</returns>
        /// <exception cref="有异常抛出">有异常抛出</exception>
        public static List<AutoUpdate.Models.T_SysUpdate> GetAllList(string where = null, SqlParameter[] paras = null)
        {
            List<AutoUpdate.Models.T_SysUpdate> list = new List<AutoUpdate.Models.T_SysUpdate>();
            string sql = "select * from T_SysUpdate where 1=1 ";
            if (where != null)
            {
                sql += where;
            }
            try
            {
                SqlDataReader reader = SQLHelper.ExecuteReader(sql, paras);
                while (reader.Read())
                {
                    AutoUpdate.Models.T_SysUpdate g = new AutoUpdate.Models.T_SysUpdate();
                    g.ID = reader["ID"] == DBNull.Value ? 0 : Convert.ToInt32(reader["ID"]);
                    g.FID = reader["FID"] == DBNull.Value ? "" : Convert.ToString(reader["FID"]);
                    g.FOrder = reader["FOrder"] == DBNull.Value ? 0 : Convert.ToInt32(reader["FOrder"]);
                    g.FProductID = reader["FProductID"] == DBNull.Value ? "" : Convert.ToString(reader["FProductID"]);
                    g.FFieldName = reader["FFieldName"] == DBNull.Value ? "" : Convert.ToString(reader["FFieldName"]);
                    g.FModifyTime = reader["FModifyTime"] == DBNull.Value ? "" : Convert.ToString(reader["FModifyTime"]);
                    g.FFileType = reader["FFileType"] == DBNull.Value ? null : (byte[])(reader["FFileType"]);
                    g.FFileSize = reader["FFileSize"] == DBNull.Value ? "" : Convert.ToString(reader["FFileSize"]);
                    g.FInstallPath = reader["FInstallPath"] == DBNull.Value ? "" : Convert.ToString(reader["FInstallPath"]);
                    g.FPublishTime = reader["FPublishTime"] == DBNull.Value ? new Nullable<DateTime>() : Convert.ToDateTime(reader["FPublishTime"]);
                    g.FLastClientIP = reader["FLastClientIP"] == DBNull.Value ? "" : Convert.ToString(reader["FLastClientIP"]);
                    g.FLastTime = reader["FLastTime"] == DBNull.Value ? new Nullable<DateTime>() : Convert.ToDateTime(reader["FLastTime"]);
                    g.FDownNum = reader["FDownNum"] == DBNull.Value ? 0 : Convert.ToInt32(reader["FDownNum"]);
                    g.FMemo = reader["FMemo"] == DBNull.Value ? "" : Convert.ToString(reader["FMemo"]);
                    g.FFlag = reader["FFlag"] == DBNull.Value ? null : (byte[])(reader["FFlag"]);
                    g.exefile = reader["exefile"] == DBNull.Value ? null : (byte[])(reader["exefile"]);
                    g.FMustUpdate = reader["FMustUpdate"] == DBNull.Value ? 0 : Convert.ToInt32(reader["FMustUpdate"]);
                    g.FNowUpdate = reader["FNowUpdate"] == DBNull.Value ? "" : Convert.ToString(reader["FNowUpdate"]);

                    list.Add(g);
                }
                reader.Close();
            }
            catch
            {

                throw;
            }
            return list;
        }
        /// <summary>
        /// 根据条件查询多个，返回DataSet集合
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="paras">参数</param>
        /// <returns>DataSet集合</returns>
        /// <exception cref="有异常抛出">有异常抛出</exception>
        public static DataSet GetAllDateSet(string where = null, SqlParameter[] paras = null)
        {
            DataSet ds = null;
            string sql = "select * from T_SysUpdate where 1 = 1 ";
            if (where != null)
            {
                sql += where;
            }
            try
            {
                ds = SQLHelper.ExecuteTable(sql, paras);
            }
            catch
            {

                throw;
            }
            return ds;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="g">要添加的对象</param>
        /// <returns>受影响行数</returns>
        /// <exception cref="有异常抛出">有异常抛出</exception>
        public static int Add(AutoUpdate.Models.T_SysUpdate g)
        {
            string sql = @"insert into T_SysUpdate(FID,FOrder,FProductID,FFieldName,FModifyTime,FFileType,FFileSize,FInstallPath,FPublishTime,FLastClientIP,FLastTime,FDownNum,FMemo,FFlag,exefile,FMustUpdate,FNowUpdate) values(@FID,@FOrder,@FProductID,@FFieldName,@FModifyTime,@FFileType,@FFileSize,@FInstallPath,@FPublishTime,@FLastClientIP,@FLastTime,@FDownNum,@FMemo,@FFlag,@exefile,@FMustUpdate,@FNowUpdate)";
            SqlParameter[] paras =
            {
                new SqlParameter("@FID",g.FID),
                new SqlParameter("@FOrder",g.FOrder),
                new SqlParameter("@FProductID",g.FProductID),
                new SqlParameter("@FFieldName",g.FFieldName),
                new SqlParameter("@FModifyTime",g.FModifyTime),
                new SqlParameter("@FFileType",g.FFileType),
                new SqlParameter("@FFileSize",g.FFileSize),
                new SqlParameter("@FInstallPath",g.FInstallPath),
                new SqlParameter("@FPublishTime",g.FPublishTime),
                new SqlParameter("@FLastClientIP",g.FLastClientIP),
                new SqlParameter("@FLastTime",g.FLastTime),
                new SqlParameter("@FDownNum",g.FDownNum),
                new SqlParameter("@FMemo",g.FMemo),
                new SqlParameter("@FFlag",g.FFlag),
                new SqlParameter("@exefile",g.exefile),
                new SqlParameter("@FMustUpdate",g.FMustUpdate),
                new SqlParameter("@FNowUpdate",g.FNowUpdate),

            };
            try
            {
                return SQLHelper.ExecuteNonQuery(sql, paras);
            }
            catch
            {

                throw;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="g">要修改后的对象</param>
        /// <returns>受影响行数</returns>
        /// <exception cref="有异常抛出">有异常抛出</exception>
        public static int Update(AutoUpdate.Models.T_SysUpdate g)
        {
            string sql = @"update T_SysUpdate set FID = @FID,FOrder = @FOrder,FProductID = @FProductID,FFieldName = @FFieldName,FModifyTime = @FModifyTime,FFileType = @FFileType,FFileSize = @FFileSize,FInstallPath = @FInstallPath,FPublishTime = @FPublishTime,FLastClientIP = @FLastClientIP,FLastTime = @FLastTime,FDownNum = @FDownNum,FMemo = @FMemo,FFlag = @FFlag,exefile = @exefile,FMustUpdate = @FMustUpdate,FNowUpdate = @FNowUpdate,xgsj = getdate() where del = 0 and ID = @ID";
            SqlParameter[] paras =
            {
                new SqlParameter("@ID",g.ID),
                new SqlParameter("@FID",g.FID),
                new SqlParameter("@FOrder",g.FOrder),
                new SqlParameter("@FProductID",g.FProductID),
                new SqlParameter("@FFieldName",g.FFieldName),
                new SqlParameter("@FModifyTime",g.FModifyTime),
                new SqlParameter("@FFileType",g.FFileType),
                new SqlParameter("@FFileSize",g.FFileSize),
                new SqlParameter("@FInstallPath",g.FInstallPath),
                new SqlParameter("@FPublishTime",g.FPublishTime),
                new SqlParameter("@FLastClientIP",g.FLastClientIP),
                new SqlParameter("@FLastTime",g.FLastTime),
                new SqlParameter("@FDownNum",g.FDownNum),
                new SqlParameter("@FMemo",g.FMemo),
                new SqlParameter("@FFlag",g.FFlag),
                new SqlParameter("@exefile",g.exefile),
                new SqlParameter("@FMustUpdate",g.FMustUpdate),
                new SqlParameter("@FNowUpdate",g.FNowUpdate),

            };
            try
            {
                return SQLHelper.ExecuteNonQuery(sql, paras);
            }
            catch
            {

                throw;
            }
        }
        /// <summary>
        /// 根据对象删除数据
        /// </summary>
        /// <param name="g">要删除的对象</param>
        /// <returns>受影响行数</returns>
        /// <exception cref="有异常抛出">有异常抛出</exception>
        public static int Delete(AutoUpdate.Models.T_SysUpdate g)
        {
            string sql = "delete T_SysUpdate where ID = @ID";
            SqlParameter[] paras =
            {
                new SqlParameter("@ID",g.ID),

            };
            try
            {
                return SQLHelper.ExecuteNonQuery(sql, paras);
            }
            catch
            {

                throw;
            }
        }
        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <param name="gradeid">主键</param>
        /// <returns>受影响行数</returns>
        /// <exception cref="有异常抛出">有异常抛出</exception>
        public static int Delete(int id)
        {
            string sql = "delete T_SysUpdate where ID = @ID";
            SqlParameter[] paras =
            {
                new SqlParameter("@ID",id)
            };
            try
            {
                return SQLHelper.ExecuteNonQuery(sql, paras);
            }
            catch
            {

                throw;
            }
        }
        /// <summary>
        /// 根据条件删除
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="paras">参数</param>
        /// <returns>受影响行数</returns>
        /// <exception cref="有异常抛出">有异常抛出</exception>
        public static int Delete(string where = null, SqlParameter[] paras = null)
        {
            string sql = "delete from T_SysUpdate where 1 = 1 ";
            if (!string.IsNullOrEmpty(where))
            {
                sql += where;
            }
            try
            {
                return SQLHelper.ExecuteNonQuery(sql, paras);
            }
            catch
            {
                throw;
            }
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="count">总页数</param>
        /// <param name="ji">要访问的页码</param>
        /// <param name="size">一页显示多少数据</param>
        /// <returns>要显示页的数据</returns>
        /// <exception cref="有异常抛出">有异常抛出</exception>
        public static List<AutoUpdate.Models.T_SysUpdate> GetByFenYe(out int count, int ji = 1, int size = 10)
        {
            string sql = "select Count(*) from T_SysUpdate";
            try
            {
                int zong = Convert.ToInt32(SQLHelper.ExecuteScalar(sql));
                count = zong % size > 0 ? zong / size + 1 : zong / size;
                sql = string.Format("select top {0} * from T_SysUpdate where ID not in (select top ({0} * {1}) ID from T_SysUpdate order by ID)order by ID", size, ji - 1);
                SqlDataReader reader = SQLHelper.ExecuteReader(sql);
                List<AutoUpdate.Models.T_SysUpdate> list = new List<AutoUpdate.Models.T_SysUpdate>();
                while (reader.Read())
                {
                    AutoUpdate.Models.T_SysUpdate g = new AutoUpdate.Models.T_SysUpdate();
                    g.ID = reader["ID"] == DBNull.Value ? 0 : Convert.ToInt32(reader["ID"]);
                    g.FID = reader["FID"] == DBNull.Value ? "" : Convert.ToString(reader["FID"]);
                    g.FOrder = reader["FOrder"] == DBNull.Value ? 0 : Convert.ToInt32(reader["FOrder"]);
                    g.FProductID = reader["FProductID"] == DBNull.Value ? "" : Convert.ToString(reader["FProductID"]);
                    g.FFieldName = reader["FFieldName"] == DBNull.Value ? "" : Convert.ToString(reader["FFieldName"]);
                    g.FModifyTime = reader["FModifyTime"] == DBNull.Value ? "" : Convert.ToString(reader["FModifyTime"]);
                    g.FFileType = reader["FFileType"] == DBNull.Value ? null : (byte[])(reader["FFileType"]);
                    g.FFileSize = reader["FFileSize"] == DBNull.Value ? "" : Convert.ToString(reader["FFileSize"]);
                    g.FInstallPath = reader["FInstallPath"] == DBNull.Value ? "" : Convert.ToString(reader["FInstallPath"]);
                    g.FPublishTime = reader["FPublishTime"] == DBNull.Value ? new Nullable<DateTime>() : Convert.ToDateTime(reader["FPublishTime"]);
                    g.FLastClientIP = reader["FLastClientIP"] == DBNull.Value ? "" : Convert.ToString(reader["FLastClientIP"]);
                    g.FLastTime = reader["FLastTime"] == DBNull.Value ? new Nullable<DateTime>() : Convert.ToDateTime(reader["FLastTime"]);
                    g.FDownNum = reader["FDownNum"] == DBNull.Value ? 0 : Convert.ToInt32(reader["FDownNum"]);
                    g.FMemo = reader["FMemo"] == DBNull.Value ? "" : Convert.ToString(reader["FMemo"]);
                    g.FFlag = reader["FFlag"] == DBNull.Value ? null : (byte[])(reader["FFlag"]);
                    g.exefile = reader["exefile"] == DBNull.Value ? null : (byte[])(reader["exefile"]);
                    g.FMustUpdate = reader["FMustUpdate"] == DBNull.Value ? 0 : Convert.ToInt32(reader["FMustUpdate"]);
                    g.FNowUpdate = reader["FNowUpdate"] == DBNull.Value ? "" : Convert.ToString(reader["FNowUpdate"]);

                    list.Add(g);
                }
                return list;
            }
            catch
            {

                throw;
            }
        }
    }
}

